<?php


class TestPush{

    function sign($arr) {
        ksort($arr);
        $str = array();
        foreach($arr as $key => $val){
            $str[] = "{$key}={$val}";
        }
        $str = implode('&',$str);
        return md5($str);
    }

    function post_get($url,$param = null,$decode = true) {
        $option = array(
            'http' => array(
                'method'        => 'POST',
                'ignore_errors' => true,
                'header' => "Content-type: application/x-www-form-urlencoded ",
            ),
        );
        if ($param) {
            $option['http']['content'] = http_build_query($param);
        }
        $context = stream_context_create($option);

        // Send the request
        $response = file_get_contents($url, false, $context);
//        $response = str_replace(':}', ':""}', $response);
//        $response = str_replace(':,', ':"",', $response);


        if (!$decode) {
            return $response;
        }

        return json_decode($response,true);

    }

    function test(){
        $data = array(
            'buyerName' => 'Me',
            'buyerMobile' => '18703736134',
            'orderSn' => '01020322333',
            'payAmount' => 100,
            'payType' => 'Weixin',
            'payTime' => time(),
            'paySn' => '1234567',
            'consignee' => 'bin',
            'idNumber' => '410703199103263035',
            'address' => 'HeNan XinXiang Test',
            'provinceCode' => '110000',
            'cityCode' => '110000',
            'areaCode' => '110101',
            'consigneeMobile' => '18703736134',
            'orderGoods' => json_encode(array(
                array(
                    'goodsId' => '420',
                    'num' => 2,
                    'price' => 10086
                )
            )),
            'userName' => 'cjw',
            'userPass' => md5(666888),
        );
        $data['sign'] = $this->sign($data);


        $url = "http://www.kr-my.com/shop/?act=convert_order";

        $res = $this->post_get($url,$data);
        if ($res['errCode'] == 0) {
            echo "推送成功";
        }
        else{
            echo "推送失败，错误代码为 {$res['errCode']},错误信息为 {$res['errMessage']}";
        }
//        print_r($res);
    }
}

$ins = new TestPush();
$ins->test();